﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Design;
using System.Drawing.Drawing2D;
using System.Reflection;
using System.Windows.Forms;
using HML.Design;

namespace HML
{
    /// <summary>
    /// 面包屑导航控件
    /// </summary>
    [Description("面包屑导航控件")]
    [DefaultProperty("Items")]
    [DefaultEvent("SelectedIndexChanged")]
    [TypeConverter(typeof(BreadcrumbBarConverter))]
    [Designer(typeof(BreadcrumbBarDesigner))]
    [ToolboxItem(true)]
    [ToolboxBitmap(typeof(BreadcrumbBar), "Controls.BreadcrumbBar.Resources.BreadcrumbBar.bmp")]
    public class BreadcrumbBar : DpiControl, ISkinObject
    {
        #region 主题

        private SkinObjectXmlMetadata skinObjectXmlMetadata = null;
        /// <summary>
        /// 主题对象在主题文件信息
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual SkinObjectXmlMetadata SkinObjectXmlMetadata
        {
            get
            {
                if (this.skinObjectXmlMetadata == null)
                    this.skinObjectXmlMetadata = new SkinObjectXmlMetadata(Assembly.GetAssembly(typeof(BreadcrumbBar)).GetName().Name, typeof(BreadcrumbBar).Name);

                return this.skinObjectXmlMetadata;
            }
        }

        private SkinController skinController = null;
        /// <summary>
        /// 主题控制器
        /// </summary>
        [Description("主题控制器")]
        [Category("杂项")]
        [PropertyOrder(-360)]
        [DefaultValue(null)]
        [RefreshProperties(RefreshProperties.All)]
        public virtual SkinController SkinController
        {
            get { return this.skinController; }
            set
            {
                if (this.skinController == value)
                    return;

                if (this.skinController != null)
                    this.skinController.RemoveSkinObject(this);

                this.skinController = value;

                if (this.skinController != null)
                    this.skinController.AddSkinObject(this);
            }
        }

        private SkinEnabledState skinEnabled = SkinEnabledState.Auto;
        /// <summary>
        /// 主题是否启用
        /// </summary>
        [Description("主题是否启用")]
        [Category("杂项")]
        [PropertyOrder(-300)]
        [DefaultValue(SkinEnabledState.Auto)]
        [RefreshProperties(RefreshProperties.All)]
        public virtual SkinEnabledState SkinEnabled
        {
            get { return this.skinEnabled; }
            set
            {
                if (this.skinEnabled == value)
                    return;

                this.skinEnabled = value;
                this.OnSkinChanged();
            }
        }

        private SkinStyle skinStyle = SkinStyle.Normal;
        /// <summary>
        /// 主题风格
        /// </summary>
        [Description("主题风格")]
        [Category("杂项")]
        [PropertyOrder(-260)]
        [DefaultValue(SkinStyle.Normal)]
        [RefreshProperties(RefreshProperties.All)]
        public virtual SkinStyle SkinStyle
        {
            get { return this.skinStyle; }
            set
            {
                if (this.skinStyle == value)
                    return;

                this.skinStyle = value;
                this.OnSkinChanged();
            }
        }

        private StyleAppearanceObject stateAppearance;
        /// <summary>
        /// 风格外观
        /// </summary>
        [Description("风格外观")]
        [PropertyOrder(-200)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public StyleAppearanceObject StyleAppearance
        {
            get
            {
                if (this.stateAppearance == null)
                    this.stateAppearance = new StyleAppearanceObject(this, null);
                return this.stateAppearance;
            }
        }

        /// <summary>
        /// 主题已更改
        /// </summary>
        public virtual void OnSkinChanged()
        {
            SkinManager.SyncSkinValueToProperty(this);
            this.Invalidate();
        }

        private ISkinObject followSkinObject = null;
        /// <summary>
        /// 主题设置跟随指定主题对象（自己的设置不再生效，提供给代码使用）
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [Localizable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public virtual ISkinObject FollowSkinObject
        {
            get { return this.followSkinObject; }
            set
            {
                if (this.followSkinObject == value)
                    return;

                this.followSkinObject = value;
                this.OnSkinChanged();
            }
        }

        public virtual bool GetSkinObjectSkinStateCore()
        {
            return SkinManager.GetSkinObjectSkinState(this.FollowSkinObject ?? this);
        }

        void ISkinObject.InitializeInvalidate()
        {
            this.InitializeElement();
            this.Invalidate();
        }

        void ISkinObject.Invalidate()
        {
            this.Invalidate();
        }

        #endregion

        #region 新增事件

        public delegate void ItemOperatingEventHandler(object sender, BreadcrumbBarItemOperatingEventArgs e);
        public delegate void ItemOperatedEventHandler(object sender, BreadcrumbBarItemOperatedEventArgs e);

        private static readonly object EventItemDeselecting = new object();
        /// <summary>
        /// 导航选项取消选中时事件
        /// </summary>
        [Description("导航选项取消选中时事件")]
        public event ItemOperatingEventHandler ItemDeselecting
        {
            add { Events.AddHandler(EventItemDeselecting, value); }
            remove { Events.RemoveHandler(EventItemDeselecting, value); }
        }

        private static readonly object EventItemDeselected = new object();
        /// <summary>
        /// 导航选项取消选中后事件
        /// </summary>
        [Description("导航选项取消选中后事件")]
        public event ItemOperatedEventHandler ItemDeselected
        {
            add { Events.AddHandler(EventItemDeselected, value); }
            remove { Events.RemoveHandler(EventItemDeselected, value); }
        }

        private static readonly object EventItemSelecting = new object();
        /// <summary>
        /// 导航选项选中时事件
        /// </summary>
        [Description("导航选项选中时事件")]
        public event ItemOperatingEventHandler ItemSelecting
        {
            add { Events.AddHandler(EventItemSelecting, value); }
            remove { Events.RemoveHandler(EventItemSelecting, value); }
        }

        private static readonly object EventItemSelected = new object();
        /// <summary>
        /// 导航选项选中后事件
        /// </summary>
        [Description("导航选项选中后事件")]
        public event ItemOperatedEventHandler ItemSelected
        {
            add { Events.AddHandler(EventItemSelected, value); }
            remove { Events.RemoveHandler(EventItemSelected, value); }
        }

        private static readonly object EventSelectedIndexChanged = new object();
        /// <summary>
        /// 导航选项选中索引更改后事件
        /// </summary>
        [Description("导航选项选中索引更改后事件")]
        public event EventHandler SelectedIndexChanged
        {
            add { Events.AddHandler(EventSelectedIndexChanged, value); }
            remove { Events.RemoveHandler(EventSelectedIndexChanged, value); }
        }

        #endregion

        #region 停用事件

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler PaddingChanged
        {
            add { base.PaddingChanged += value; }
            remove { base.PaddingChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler TextChanged
        {
            add { base.TextChanged += value; }
            remove { base.TextChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler FontChanged
        {
            add { base.FontChanged += value; }
            remove { base.FontChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler RightToLeftChanged
        {
            add { base.RightToLeftChanged += value; }
            remove { base.RightToLeftChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler ImeModeChanged
        {
            add { base.ImeModeChanged += value; }
            remove { base.ImeModeChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler BackgroundImageChanged
        {
            add { base.BackgroundImageChanged += value; }
            remove { base.BackgroundImageChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler BackgroundImageLayoutChanged
        {
            add { base.BackgroundImageLayoutChanged += value; }
            remove { base.BackgroundImageLayoutChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler BackColorChanged
        {
            add { base.BackColorChanged += value; }
            remove { base.BackColorChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler ForeColorChanged
        {
            add { base.ForeColorChanged += value; }
            remove { base.ForeColorChanged -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler Click
        {
            add { base.Click += value; }
            remove { base.Click -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event EventHandler DoubleClick
        {
            add { base.DoubleClick += value; }
            remove { base.DoubleClick -= value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new event MouseEventHandler MouseDoubleClick
        {
            add { base.MouseDoubleClick += value; }
            remove { base.MouseDoubleClick -= value; }
        }
        #endregion

        #region 新增属性

        private BreadcrumbBarStylePattern stylePattern = BreadcrumbBarStylePattern.Arrows;
        /// <summary>
        /// 风格模式
        /// </summary>
        [Description("风格模式")]
        [Category("杂项")]
        [PropertyOrder(-184)]
        [DefaultValue(BreadcrumbBarStylePattern.Arrows)]
        public BreadcrumbBarStylePattern StylePattern
        {
            get { return this.stylePattern; }
            set
            {
                if (this.stylePattern == value)
                    return;

                this.stylePattern = value;
                this.InitializeElement();
                this.Invalidate();
            }
        }

        private bool itemAutoWidth = false;
        /// <summary>
        ///选项是否自动宽度
        /// </summary>
        [Description("选项是否自动宽度")]
        [Category("杂项")]
        [PropertyOrder(-168)]
        [DefaultValue(false)]
        public bool ItemAutoWidth
        {
            get { return this.itemAutoWidth; }
            set
            {
                if (this.itemAutoWidth == value)
                    return;

                this.itemAutoWidth = value;
                this.InitializeElement();
                this.Invalidate();
            }
        }

        private int itemAutoMaxWidth = -1;
        /// <summary>
        ///选项最大宽度，-1不限制(自动宽度模式下)
        /// </summary>
        [Description("选项最大宽度，-1不限制(自动宽度模式下)")]
        [Category("杂项")]
        [PropertyOrder(-166)]
        [DefaultValue(-1)]
        public int ItemAutoMaxWidth
        {
            get { return this.itemAutoMaxWidth; }
            set
            {
                if (this.itemAutoMaxWidth == value || value < -1)
                    return;

                this.itemAutoMaxWidth = value;
                this.InitializeElement();
                this.Invalidate();
            }
        }

        private int itemAutoMinWidth = 60;
        /// <summary>
        ///选项最小宽度(自动宽度模式下)
        /// </summary>
        [Description("选项最小宽度(自动宽度模式下)")]
        [Category("杂项")]
        [PropertyOrder(-164)]
        [DefaultValue(60)]
        public int ItemAutoMinWidth
        {
            get { return this.itemAutoMinWidth; }
            set
            {
                if (this.itemAutoMinWidth == value || value <= 0)
                    return;

                this.itemAutoMinWidth = value;
                this.InitializeElement();
                this.Invalidate();
            }
        }

        private LRMargin itemAutoWidthTextLRMargin = new LRMargin(4, 4);
        /// <summary>
        /// 选项文本左右外边距(自动宽度模式下)
        /// </summary>
        [Description("选项文本左右外边距(自动宽度模式下)")]
        [Category("杂项")]
        [PropertyOrder(-162)]
        [DefaultValue(typeof(LRMargin), "4, 4")]
        public LRMargin ItemAutoWidthTextLRMargin
        {
            get { return this.itemAutoWidthTextLRMargin; }
            set
            {
                if (value.Left <= 0)
                    value.Left = 0;
                if (value.Right <= 0)
                    value.Right = 0;

                if (this.itemAutoWidthTextLRMargin == value)
                    return;

                this.itemAutoWidthTextLRMargin = value;

                this.Height = this.GetControlAutoHeight();
                this.InitializeElement();
                this.Invalidate();
            }
        }

        private int itemFixedWidth = 80;
        /// <summary>
        ///选项固定宽度
        /// </summary>
        [Description("选项自动宽度")]
        [Category("杂项")]
        [PropertyOrder(-161)]
        [DefaultValue(80)]
        public int ItemFixedWidth
        {
            get { return this.itemFixedWidth; }
            set
            {
                if (this.itemFixedWidth == value)
                    return;

                this.itemFixedWidth = value;
                this.InitializeElement();
                this.Invalidate();
            }
        }

        private bool itemFirstBespread = false;
        /// <summary>
        ///第一个选项是否填满(只限于 Arrows、Parallelogram、RoundCap、Leaf)
        /// </summary>
        [Description("第一个选项是否填满(只限于 Arrows、Parallelogram、RoundCap、Leaf)")]
        [Category("杂项")]
        [PropertyOrder(-160)]
        [DefaultValue(false)]
        public bool ItemFirstBespread
        {
            get { return this.itemFirstBespread; }
            set
            {
                if (this.itemFirstBespread == value)
                    return;

                this.itemFirstBespread = value;
                this.InitializeElement();
                this.Invalidate();
            }
        }

        private int itemDistance = 2;
        /// <summary>
        ///选项间隔距离
        /// </summary>
        [Description("选项间隔距离")]
        [Category("杂项")]
        [PropertyOrder(-158)]
        [DefaultValue(2)]
        public int ItemDistance
        {
            get { return this.itemDistance; }
            set
            {
                if (this.itemDistance == value || value < 0)
                    return;

                this.itemDistance = value;
                this.InitializeElement();
                this.Invalidate();
            }
        }

        private TBMargin itemTextTBMargin = new TBMargin(4, 4);
        /// <summary>
        /// 选项文本上下外边距
        /// </summary>
        [Description("选项文本上下外边距")]
        [Category("杂项")]
        [PropertyOrder(-156)]
        [DefaultValue(typeof(TBMargin), "4, 4")]
        public TBMargin ItemTextTBMargin
        {
            get { return this.itemTextTBMargin; }
            set
            {
                if (value.Top <= 0)
                    value.Top = 0;
                if (value.Bottom <= 0)
                    value.Bottom = 0;

                if (this.itemTextTBMargin == value)
                    return;

                this.itemTextTBMargin = value;

                this.Height = this.GetControlAutoHeight();
                this.InitializeElement();
                this.Invalidate();
            }
        }

        private BreadcrumbBarItemCollection navigationBarItemCollection;
        /// <summary>
        /// 选项集合
        /// </summary>
        [Description("选项集合")]
        [Category("杂项")]
        [PropertyOrder(-140)]
        [DefaultValue(null)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public BreadcrumbBarItemCollection Items
        {
            get
            {
                if (this.navigationBarItemCollection == null)
                    this.navigationBarItemCollection = new BreadcrumbBarItemCollection(this);
                return this.navigationBarItemCollection;
            }
        }

        private int selectedIndex = -1;
        /// <summary>
        /// 已选中选项索引
        /// </summary>
        [DefaultValue("-1")]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public int SelectedIndex
        {
            get { return this.selectedIndex; }
            set
            {
                if (this.selectedIndex == value || value < -1 || value >= this.Items.Count)
                    return;

                int old_itemIndex = this.selectedIndex;
                BreadcrumbBarItem old_item = this.Items.Find(this.selectedIndex);
                if (old_item != null)
                {
                    //旧已选中选项取消中事件
                    BreadcrumbBarItemOperatingEventArgs deselecting_arg = new BreadcrumbBarItemOperatingEventArgs(old_item);
                    this.OnItemDeselecting(deselecting_arg);
                    if (deselecting_arg.Cancel)
                    {
                        return;
                    }

                    //旧已选中选项取消后事件
                    BreadcrumbBarItemOperatedEventArgs deselected_arg = new BreadcrumbBarItemOperatedEventArgs(old_item);
                    this.OnItemDeselected(deselected_arg);
                }

                this.selectedIndex = value;
                BreadcrumbBarItem new_item = this.Items.Find(this.selectedIndex);
                if (new_item != null)
                {
                    // 新选项选中中事件
                    BreadcrumbBarItemOperatingEventArgs selecting_arg = new BreadcrumbBarItemOperatingEventArgs(new_item);
                    this.OnItemSelecting(selecting_arg);
                    if (selecting_arg.Cancel)
                    {
                        this.selectedIndex = old_itemIndex;
                        return;
                    }

                    // 新选项选中后事件
                    BreadcrumbBarItemOperatedEventArgs selected_arg = new BreadcrumbBarItemOperatedEventArgs(new_item);
                    this.OnItemSelected(selected_arg);
                }

                //选中索引更改事件
                this.OnSelectedIndexChanged(new BreadcrumbBarItemOperatedEventArgs(this.SelectedItem));

                this.Invalidate();
            }
        }

        /// <summary>
        /// 已选中选项
        /// </summary>
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public BreadcrumbBarItem SelectedItem
        {
            get
            {
                if (this.SelectedIndex < 0 || this.SelectedIndex >= this.Items.Count)
                {
                    return null;
                }
                return this.Items[this.SelectedIndex];
            }
            set
            {
                int index = this.Items.IndexOf(value);
                if (index < 0 || index >= this.Items.Count)
                {
                    return;
                }
                this.SelectedIndex = index;
            }
        }

        #endregion

        #region 重写属性

        protected override Size DefaultSize
        {
            get { return new Size(240, 24); }
        }

        protected override ImeMode DefaultImeMode
        {
            get { return ImeMode.Disable; }
        }

        public override Font Font
        {
            get { return base.Font; }
            set
            {
                if (base.Font == value)
                    return;

                base.Font = value;
                this.Height = this.GetControlAutoHeight();
                this.Invalidate();
            }
        }

        #endregion

        #region 停用属性

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public new Padding Padding
        {
            get { return base.Padding; }
            set { base.Padding = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override string Text
        {
            get { return base.Text; }
            set { base.Text = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public override RightToLeft RightToLeft
        {
            get { return base.RightToLeft; }
            set { base.RightToLeft = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        public new ImeMode ImeMode
        {
            get { return base.ImeMode; }
            set { base.ImeMode = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Image BackgroundImage
        {
            get { return base.BackgroundImage; }
            set { base.BackgroundImage = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override ImageLayout BackgroundImageLayout
        {
            get { return base.BackgroundImageLayout; }
            set { base.BackgroundImageLayout = value; }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Color BackColor
        {
            get { return this.StyleAppearance.BackColor; }
            set { }
        }

        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public override Color ForeColor
        {
            get { return this.StyleAppearance.UnSelected.Normal.TextColor; }
            set { }
        }
        #endregion

        #region 字段

        /// <summary>
        /// 控件激活显示状态（配合Focused 、ShowFocusCues、activatedIndex使用）
        /// </summary>
        private bool activatedState = false;
        /// <summary>
        /// 控件激活状态选项索引(-1 没有、>=0 选项)
        /// </summary>
        private int activatedIndex = -1;

        /// <summary>
        /// 当前鼠标已按下的对象
        /// </summary>
        private object mousedownobject = null;
        /// <summary>
        /// 当前鼠标已进入的对象
        /// </summary>
        private object mouseenterobject = null;

        /// <summary>
        /// 开启抗锯齿功能xy都会增大1px
        /// </summary>
        private int content_padding = 1;

        #endregion

        public BreadcrumbBar()
        {
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.ResizeRedraw, true);
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.StandardClick, true);
            SetStyle(ControlStyles.StandardDoubleClick, false);

            this.OnSkinChanged();
        }

        #region 重写

        protected override void OnScaleDpiChangedInitialize()
        {
            this.InitializeElement();
            this.Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            Graphics g = e.Graphics;
            g.SmoothingMode = SmoothingMode.AntiAlias;

            SolidBrush commom_back_sb = new SolidBrush(Color.White);
            SolidBrush commom_text_sb = new SolidBrush(Color.White);
            Pen symbol_pen = new Pen(this.Enabled ? this.StyleAppearance.UnSelected.Normal.SymbolColor : this.StyleAppearance.UnSelected.Disabled.SymbolColor);

            //选项
            for (int i = 0; i < this.Items.Count; i++)
            {
                bool isselected = (this.SelectedItem == this.Items[i]);
                StyleAppearanceObject.StateAppearanceObject stateAppearanceObject = isselected ? this.StyleAppearance.Selected : this.StyleAppearance.UnSelected;
                BreadcrumbBarItem.StyleObject.StateObject itemStateAppearanceObject = isselected ? this.Items[i].StyleAppearance.Selected : this.Items[i].StyleAppearance.UnSelected;

                if (!this.Enabled)
                {
                    commom_back_sb.Color = itemStateAppearanceObject.Disabled.BackColor != Color.Empty ? itemStateAppearanceObject.Disabled.BackColor : stateAppearanceObject.Disabled.BackColor;
                    commom_text_sb.Color = itemStateAppearanceObject.Disabled.BackColor != Color.Empty ? itemStateAppearanceObject.Disabled.TextColor : stateAppearanceObject.Disabled.TextColor;
                }
                else if (this.Items[i].IsMouseEnter)
                {
                    commom_back_sb.Color = itemStateAppearanceObject.Enter.BackColor != Color.Empty ? itemStateAppearanceObject.Enter.BackColor : stateAppearanceObject.Enter.BackColor;
                    commom_text_sb.Color = itemStateAppearanceObject.Enter.BackColor != Color.Empty ? itemStateAppearanceObject.Enter.TextColor : stateAppearanceObject.Enter.TextColor;
                }
                else
                {
                    commom_back_sb.Color = itemStateAppearanceObject.Normal.BackColor != Color.Empty ? itemStateAppearanceObject.Normal.BackColor : stateAppearanceObject.Normal.BackColor;
                    commom_text_sb.Color = itemStateAppearanceObject.Normal.BackColor != Color.Empty ? itemStateAppearanceObject.Normal.TextColor : stateAppearanceObject.Normal.TextColor;
                }

                this.DrawItem(g, this.Items[i], i, commom_back_sb, commom_text_sb, symbol_pen);

            }

            commom_back_sb.Dispose();
            commom_text_sb.Dispose();
            symbol_pen.Dispose();
        }

        protected override void Select(bool directed, bool forward)
        {
            base.Select(directed, forward);

            this.activatedState = true;
            this.activatedIndex = 0;
            this.Invalidate();
        }

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);

            this.mouseenterobject = null;
            this.activatedIndex = 0;
            this.Invalidate();
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);

            this.mousedownobject = null;
            this.activatedState = false;
            this.Invalidate();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);

            this.mouseenterobject = null;
            this.Invalidate();
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            if (!this.Focused)
            {
                this.Focus();
            }

            if (e.Button == MouseButtons.Left)
            {
                object mousedownobject_tmp = null;
                for (int i = 0; i < this.Items.Count; i++)
                {
                    if (this.Items[i].BackRectF.Contains(e.Location))
                    {
                        mousedownobject_tmp = this.Items[i];
                        this.activatedIndex = i;
                        this.Invalidate();
                        break;
                    }
                }

                this.mousedownobject = mousedownobject_tmp;
            }

            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (this.mousedownobject != null)
                {
                    for (int i = 0; i < this.Items.Count; i++)
                    {
                        if (this.Items[i].BackRectF.Contains(e.Location) && this.mousedownobject == this.Items[i])
                        {
                            this.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
            this.mousedownobject = null;

            base.OnMouseUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);

            if (this.mousedownobject == null)
            {
                object mouseenterobject_tmp = null;
                for (int i = 0; i < this.Items.Count; i++)
                {
                    if (this.Items[i].BackRectF.Contains(e.Location))
                    {
                        mouseenterobject_tmp = this.Items[i];
                        break;
                    }
                }
                if (this.mouseenterobject != mouseenterobject_tmp)
                {
                    this.mouseenterobject = mouseenterobject_tmp;
                    this.Invalidate();
                }
            }
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            switch (keyData)
            {
                case Keys.Left:
                    {
                        this.activatedState = true;

                        this.activatedIndex--;
                        if (this.activatedIndex < 0)
                        {
                            this.activatedIndex = this.Items.Count - 1;
                        }
                        this.Invalidate();
                        return true;
                    }
                case Keys.Right:
                    {
                        this.activatedState = true;

                        this.activatedIndex++;
                        if (this.activatedIndex > this.Items.Count - 1)
                        {
                            this.activatedIndex = 0;
                        }
                        this.Invalidate();
                        return true;
                    }
                case Keys.Enter:
                case Keys.Space:
                    {
                        if (this.activatedIndex > -1 && this.activatedIndex < this.Items.Count)
                        {
                            this.SelectedIndex = this.activatedIndex;
                        }
                        return true;
                    }
            }

            return base.ProcessDialogKey(keyData);
        }

        protected override void SetBoundsCore(int x, int y, int width, int height, BoundsSpecified specified)
        {
            height = this.GetControlAutoHeight();

            base.SetBoundsCore(x, y, width, height, specified);
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);

            this.InitializeElement();
            this.Invalidate();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this.Items != null)
                {
                    for (int i = 0; i < this.Items.Count; i++)
                    {
                        if (this.Items[i].Path != null)
                        {
                            this.Items[i].Path.Dispose();
                        }
                    }
                }
            }
            base.Dispose(disposing);
        }

        #endregion

        #region 虚方法

        protected virtual void OnItemDeselecting(BreadcrumbBarItemOperatingEventArgs e)
        {
            ItemOperatingEventHandler handler = Events[EventItemDeselecting] as ItemOperatingEventHandler;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnItemDeselected(BreadcrumbBarItemOperatedEventArgs e)
        {
            ItemOperatedEventHandler handler = Events[EventItemDeselected] as ItemOperatedEventHandler;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnItemSelecting(BreadcrumbBarItemOperatingEventArgs e)
        {
            ItemOperatingEventHandler handler = Events[EventItemSelecting] as ItemOperatingEventHandler;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        protected virtual void OnItemSelected(BreadcrumbBarItemOperatedEventArgs e)
        {
            ItemOperatedEventHandler handler = Events[EventItemSelected] as ItemOperatedEventHandler;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        internal virtual void OnSelectedIndexChanged(EventArgs e)
        {
            EventHandler handler = Events[EventSelectedIndexChanged] as EventHandler;
            if (handler != null)
            {
                handler(this, e);
            }
        }

        #endregion

        #region 公开方法

        /// <summary>
        /// 鼠标是否进入选项对象
        /// </summary>
        /// <param name="item">选项对象</param>
        /// <returns></returns>
        internal bool IsMouseEnter(BreadcrumbBarItem item)
        {
            return (item != null && this.mouseenterobject == item);
        }

        #endregion 

        #region 公开方法

        /// <summary>
        /// 初始化控件内部元素布局
        /// </summary>
        public void InitializeElement()
        {
            NativeMethods.TEXTMETRIC font_metrics = ControlHelper.GetFontMetrics(this.Handle, this.Font);

            float scale_itemDistance = this.ItemDistance * this.ScaleDpi;
            float scale_itemMinWidth = this.ItemAutoMinWidth * this.ScaleDpi;
            float scale_itemMaxWidth = this.ItemAutoMaxWidth * this.ScaleDpi;
            float scale_left_margin = this.ItemAutoWidthTextLRMargin.Left * this.ScaleDpi;
            float scale_right_margin = this.ItemAutoWidthTextLRMargin.Right * this.ScaleDpi;
            RectangleF rect = this.ClientRectangle;

            float item_x = this.content_padding;//选项内容x坐标
            float item_y = this.content_padding;//选项内容y坐标
            float item_h = rect.Height - this.content_padding * 2;//选项内容高度

            IntPtr hDC = IntPtr.Zero;
            Graphics g = null;
            ControlHelper.GetWindowClientGraphics(this.Handle, out g, out hDC);
            for (int i = 0; i < this.Items.Count; i++)
            {
                Size text_size = new Size((int)Math.Ceiling(g.MeasureString(this.Items[i].Text, this.Font, int.MaxValue, StringFormat.GenericTypographic).Width), font_metrics.GetFontRealHeight());

                this.Items[i].TextSize = text_size;
            }
            g.Dispose();
            NativeMethods.ReleaseDC(this.Handle, hDC);

            if (this.StylePattern == BreadcrumbBarStylePattern.Arrows)//选项宽度=外边距+内容+选项高度
            {
                for (int i = 0; i < this.Items.Count; i++)
                {
                    float item_w = this.ItemFixedWidth;
                    if (this.ItemAutoWidth)
                    {
                        item_w = scale_left_margin + this.Items[i].TextSize.Width + scale_right_margin + item_h;
                        if (this.ItemAutoMaxWidth != -1)
                        {
                            item_w = Math.Min(item_w, scale_itemMaxWidth);
                        }
                        item_w = Math.Max(item_w, scale_itemMinWidth);
                    }
                    this.Items[i].BackRectF = new RectangleF(item_x, item_y, item_w, item_h);
                    item_x = this.Items[i].BackRectF.Right - item_h / 2f + scale_itemDistance;

                    GraphicsPath back_gp = new GraphicsPath();
                    if (i == 0 && this.ItemFirstBespread)
                    {
                        back_gp.AddPolygon(new PointF[] {
                                                     new PointF(this.Items[i].BackRectF.X, this.Items[i].BackRectF.Y),
                                                     new PointF(this.Items[i].BackRectF.Right- this.Items[i].BackRectF.Height / 2, this.Items[i].BackRectF.Y),
                                                     new PointF(this.Items[i].BackRectF.Right, this.Items[i].BackRectF.Y+this.Items[i].BackRectF.Height / 2),
                                                     new PointF(this.Items[i].BackRectF.Right- this.Items[i].BackRectF.Height / 2, this.Items[i].BackRectF.Bottom),
                                                     new PointF(this.Items[i].BackRectF.X , this.Items[i].BackRectF.Bottom),
                                                      });
                    }
                    else
                    {
                        back_gp.AddPolygon(new PointF[] {
                                                     new PointF(this.Items[i].BackRectF.X, this.Items[i].BackRectF.Y),
                                                     new PointF(this.Items[i].BackRectF.Right- this.Items[i].BackRectF.Height / 2, this.Items[i].BackRectF.Y),
                                                     new PointF(this.Items[i].BackRectF.Right, this.Items[i].BackRectF.Y+this.Items[i].BackRectF.Height / 2),
                                                     new PointF(this.Items[i].BackRectF.Right- this.Items[i].BackRectF.Height / 2, this.Items[i].BackRectF.Bottom),
                                                     new PointF(this.Items[i].BackRectF.X , this.Items[i].BackRectF.Bottom),
                                                     new PointF(this.Items[i].BackRectF.X+this.Items[i].BackRectF.Height / 2 , this.Items[i].BackRectF.Y+this.Items[i].BackRectF.Height / 2)
                                                     });
                    }

                    back_gp.CloseFigure();
                    GraphicsPath tmp = this.Items[i].Path;
                    this.Items[i].Path = back_gp;
                    if (tmp != null)
                    {
                        tmp.Dispose();
                    }
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.Parallelogram)//选项宽度=外边距+内容+选项高度
            {
                for (int i = 0; i < this.Items.Count; i++)
                {
                    float item_w = this.ItemFixedWidth;
                    if (this.ItemAutoWidth)
                    {
                        item_w = scale_left_margin + this.Items[i].TextSize.Width + scale_right_margin + item_h;
                        if (this.ItemAutoMaxWidth != -1)
                        {
                            item_w = Math.Min(item_w, scale_itemMaxWidth);
                        }
                        item_w = Math.Max(item_w, scale_itemMinWidth);
                    }
                    this.Items[i].BackRectF = new RectangleF(item_x, item_y, item_w, item_h);
                    item_x = this.Items[i].BackRectF.Right - item_h / 2f + scale_itemDistance;

                    GraphicsPath back_gp = new GraphicsPath();
                    if (i == 0 && this.ItemFirstBespread)
                    {
                        back_gp.AddPolygon(new PointF[] {
                                                     new PointF(this.Items[i].BackRectF.X, this.Items[i].BackRectF.Y),
                                                     new PointF(this.Items[i].BackRectF.Right - this.Items[i].BackRectF.Height / 2, this.Items[i].BackRectF.Y),
                                                     new PointF(this.Items[i].BackRectF.Right , this.Items[i].BackRectF.Bottom),
                                                     new PointF(this.Items[i].BackRectF.X, this.Items[i].BackRectF.Bottom)
                                                     });
                    }
                    else
                    {
                        back_gp.AddPolygon(new PointF[] {
                                                     new PointF(this.Items[i].BackRectF.X, this.Items[i].BackRectF.Y),
                                                     new PointF(this.Items[i].BackRectF.Right - this.Items[i].BackRectF.Height / 2, this.Items[i].BackRectF.Y),
                                                     new PointF(this.Items[i].BackRectF.Right , this.Items[i].BackRectF.Bottom),
                                                     new PointF(this.Items[i].BackRectF.X + this.Items[i].BackRectF.Height / 2, this.Items[i].BackRectF.Bottom)
                                                     });
                    }
                    back_gp.CloseFigure();
                    GraphicsPath tmp = this.Items[i].Path;
                    this.Items[i].Path = back_gp;
                    if (tmp != null)
                    {
                        tmp.Dispose();
                    }
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.RoundCap)//选项宽度=外边距+内容+选项高度
            {
                for (int i = 0; i < this.Items.Count; i++)
                {
                    float item_w = this.ItemFixedWidth;
                    if (this.ItemAutoWidth)
                    {
                        item_w = scale_left_margin + this.Items[i].TextSize.Width + scale_right_margin + item_h;
                        if (this.ItemAutoMaxWidth != -1)
                        {
                            item_w = Math.Min(item_w, scale_itemMaxWidth);
                        }
                        item_w = Math.Max(item_w, scale_itemMinWidth);
                    }
                    this.Items[i].BackRectF = new RectangleF(item_x, item_y, item_w, item_h);
                    item_x = this.Items[i].BackRectF.Right - item_h / 2f + scale_itemDistance;

                    GraphicsPath back_gp = new GraphicsPath();
                    if (i == 0 && this.ItemFirstBespread)
                    {
                        back_gp.AddLine(new PointF(this.Items[i].BackRectF.X, this.Items[i].BackRectF.Bottom), new PointF(this.Items[i].BackRectF.X, this.Items[i].BackRectF.Y));
                        back_gp.AddArc(new RectangleF(this.Items[i].BackRectF.Right - this.Items[i].BackRectF.Height, this.Items[i].BackRectF.Y, this.Items[i].BackRectF.Height, this.Items[i].BackRectF.Height), 270, 180);
                    }
                    else
                    {
                        back_gp.AddArc(new RectangleF(this.Items[i].BackRectF.X - this.Items[i].BackRectF.Height / 2, this.Items[i].BackRectF.Y, this.Items[i].BackRectF.Height, this.Items[i].BackRectF.Height), 270, 180);
                        back_gp.Reverse();
                        back_gp.AddArc(new RectangleF(this.Items[i].BackRectF.Right - this.Items[i].BackRectF.Height, this.Items[i].BackRectF.Y, this.Items[i].BackRectF.Height, this.Items[i].BackRectF.Height), 270, 180);
                    }
                    back_gp.CloseFigure();
                    GraphicsPath tmp = this.Items[i].Path;
                    this.Items[i].Path = back_gp;
                    if (tmp != null)
                    {
                        tmp.Dispose();
                    }
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.Leaf)//选项宽度=外边距+内容+选项高度*2
            {
                for (int i = 0; i < this.Items.Count; i++)
                {
                    float item_w = this.ItemFixedWidth;
                    if (this.ItemAutoWidth)
                    {
                        item_w = scale_left_margin + this.Items[i].TextSize.Width + scale_right_margin + item_h * 2;
                        if (this.ItemAutoMaxWidth != -1)
                        {
                            item_w = Math.Min(item_w, scale_itemMaxWidth);
                        }
                        item_w = Math.Max(item_w, scale_itemMinWidth);
                    }
                    this.Items[i].BackRectF = new RectangleF(item_x, item_y, item_w, item_h);
                    item_x = this.Items[i].BackRectF.Right - this.Items[i].BackRectF.Height / 5 + scale_itemDistance;

                    GraphicsPath back_gp = new GraphicsPath();
                    if (i == 0 && this.ItemFirstBespread)
                    {
                        back_gp.AddLine(new PointF(this.Items[i].BackRectF.X, this.Items[i].BackRectF.Bottom), new PointF(this.Items[i].BackRectF.X, this.Items[i].BackRectF.Y));
                        back_gp.AddArc(new RectangleF(this.Items[i].BackRectF.Right - this.Items[i].BackRectF.Height * 2, this.Items[i].BackRectF.Y, this.Items[i].BackRectF.Height * 2, this.Items[i].BackRectF.Height * 2), 270, 90);
                    }
                    else
                    {
                        back_gp.AddArc(new RectangleF(this.Items[i].BackRectF.Right - this.Items[i].BackRectF.Height * 2, this.Items[i].BackRectF.Y, this.Items[i].BackRectF.Height * 2, this.Items[i].BackRectF.Height * 2), 270, 90);
                        back_gp.AddArc(new RectangleF(this.Items[i].BackRectF.X, this.Items[i].BackRectF.Y - this.Items[i].BackRectF.Height, this.Items[i].BackRectF.Height * 2, this.Items[i].BackRectF.Height * 2), 90, 90);
                    }
                    back_gp.CloseFigure();
                    GraphicsPath tmp = this.Items[i].Path;
                    this.Items[i].Path = back_gp;
                    if (tmp != null)
                    {
                        tmp.Dispose();
                    }
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.Quadrangle)//选项宽度=外边距+内容
            {
                for (int i = 0; i < this.Items.Count; i++)
                {
                    float item_w = this.ItemFixedWidth;
                    if (this.ItemAutoWidth)
                    {
                        item_w = scale_left_margin + this.Items[i].TextSize.Width + scale_right_margin;
                        if (this.ItemAutoMaxWidth != -1)
                        {
                            item_w = Math.Min(item_w, scale_itemMaxWidth);
                        }
                        item_w = Math.Max(item_w, scale_itemMinWidth);
                    }
                    this.Items[i].BackRectF = new RectangleF(item_x, item_y, item_w, item_h);
                    item_x = this.Items[i].BackRectF.Right + scale_itemDistance;
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.Symbol)//选项宽度=外边距+内容
            {
                for (int i = 0; i < this.Items.Count; i++)
                {
                    float item_w = this.ItemFixedWidth;
                    if (this.ItemAutoWidth)
                    {
                        item_w = scale_left_margin + this.Items[i].TextSize.Width + scale_right_margin;
                        if (this.ItemAutoMaxWidth != -1)
                        {
                            item_w = Math.Min(item_w, scale_itemMaxWidth);
                        }
                        item_w = Math.Max(item_w, scale_itemMinWidth);
                    }
                    this.Items[i].BackRectF = new RectangleF(item_x, item_y, item_w, item_h);
                    float symbol_h = font_metrics.GetFontRealHeight();
                    float symbol_w = symbol_h * 2 / 3f;
                    this.Items[i].SymbolRectF = new RectangleF(this.Items[i].BackRectF.Right + scale_itemDistance / 2, item_y + (this.Items[i].BackRectF.Height - symbol_h) / 2F, symbol_w, symbol_h);
                    item_x = this.Items[i].SymbolRectF.Right + scale_itemDistance / 2;
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.ObliqueLine)//选项宽度=外边距+内容
            {
                for (int i = 0; i < this.Items.Count; i++)
                {
                    float item_w = this.ItemFixedWidth;
                    if (this.ItemAutoWidth)
                    {
                        item_w = scale_left_margin + this.Items[i].TextSize.Width + scale_right_margin;
                        if (this.ItemAutoMaxWidth != -1)
                        {
                            item_w = Math.Min(item_w, scale_itemMaxWidth);
                        }
                        item_w = Math.Max(item_w, scale_itemMinWidth);
                    }
                    this.Items[i].BackRectF = new RectangleF(item_x, item_y, item_w, item_h);
                    float symbol_h = font_metrics.GetFontRealHeight();
                    float symbol_w = symbol_h * 2 / 3f;
                    this.Items[i].SymbolRectF = new RectangleF(this.Items[i].BackRectF.Right + scale_itemDistance / 2, item_y + (this.Items[i].BackRectF.Height - symbol_h) / 2F, symbol_w, symbol_h);
                    item_x = this.Items[i].SymbolRectF.Right + scale_itemDistance / 2;
                }
            }

        }

        #endregion 

        #region 私有方法

        /// <summary>
        /// 绘制选项
        /// </summary>>
        /// <param name="g"></param>
        /// <param name="item"></param>
        /// <param name="itemIndex"></param>
        /// <param name="back_sb"></param>
        /// <param name="text_sb"></param>
        /// <param name="symbol_pen"></param>
        private void DrawItem(Graphics g, BreadcrumbBarItem item, int itemIndex, SolidBrush back_sb, SolidBrush text_sb, Pen symbol_pen)
        {
            //控件激活状态虚线框
            bool isactivated = (this.Focused && (this.ShowFocusCues || this.activatedState) && this.activatedIndex == itemIndex);
            float scale_top_margin = this.ItemTextTBMargin.Top * this.ScaleDpi;
            float scale_left_margin = this.ItemAutoWidthTextLRMargin.Left * this.ScaleDpi;

            if (this.StylePattern == BreadcrumbBarStylePattern.Arrows)
            {
                g.FillPath(back_sb, item.Path);

                Region source_r = g.Clip;
                Region current_r = new Region(item.Path);
                g.Clip = current_r;
                float x = this.ItemAutoWidth ? item.BackRectF.X + item.BackRectF.Height / 2 + scale_left_margin : item.BackRectF.X + (item.BackRectF.Width - item.TextSize.Width) / 2;
                g.DrawString(item.Text, this.Font, text_sb, x, item.BackRectF.Y + scale_top_margin, StringFormat.GenericTypographic);
                g.Clip = source_r;
                current_r.Dispose();

                if (isactivated)
                {
                    Pen activated_border_pen = new Pen(this.StyleAppearance.ActivateColor) { DashStyle = DashStyle.Dot };
                    g.DrawPath(activated_border_pen, item.Path);
                    activated_border_pen.Dispose();
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.Parallelogram)
            {
                g.FillPath(back_sb, item.Path);

                Region source_r = g.Clip;
                Region current_r = new Region(item.Path);
                g.Clip = current_r;
                float x = this.ItemAutoWidth ? item.BackRectF.X + item.BackRectF.Height / 2 + scale_left_margin : item.BackRectF.X + (item.BackRectF.Width - item.TextSize.Width) / 2;
                g.DrawString(item.Text, this.Font, text_sb, x, item.BackRectF.Y + scale_top_margin, StringFormat.GenericTypographic);
                g.Clip = source_r;
                current_r.Dispose();

                if (isactivated)
                {
                    Pen activated_border_pen = new Pen(this.StyleAppearance.ActivateColor) { DashStyle = DashStyle.Dash };
                    g.DrawPath(activated_border_pen, item.Path);
                    activated_border_pen.Dispose();
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.RoundCap)
            {
                g.FillPath(back_sb, item.Path);

                Region source_r = g.Clip;
                Region current_r = new Region(item.Path);
                g.Clip = current_r;
                float x = this.ItemAutoWidth ? item.BackRectF.X + item.BackRectF.Height / 2 + scale_left_margin : item.BackRectF.X + (item.BackRectF.Width - item.TextSize.Width) / 2;
                g.DrawString(item.Text, this.Font, text_sb, x, item.BackRectF.Y + scale_top_margin, StringFormat.GenericTypographic);
                g.Clip = source_r;
                current_r.Dispose();

                if (isactivated)
                {
                    Pen activated_border_pen = new Pen(this.StyleAppearance.ActivateColor) { DashStyle = DashStyle.Dash };
                    g.DrawPath(activated_border_pen, item.Path);
                    activated_border_pen.Dispose();
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.Leaf)
            {
                g.FillPath(back_sb, item.Path);

                Region source_r = g.Clip;
                Region current_r = new Region(item.Path);
                g.Clip = current_r;
                float x = this.ItemAutoWidth ? item.BackRectF.X + item.BackRectF.Height / 2 + scale_left_margin : item.BackRectF.X + (item.BackRectF.Width - item.TextSize.Width) / 2;
                g.DrawString(item.Text, this.Font, text_sb, x, item.BackRectF.Y + scale_top_margin, StringFormat.GenericTypographic);
                g.Clip = source_r;
                current_r.Dispose();

                if (isactivated)
                {
                    Pen activated_border_pen = new Pen(this.StyleAppearance.ActivateColor) { DashStyle = DashStyle.Dash };
                    g.DrawPath(activated_border_pen, item.Path);
                    activated_border_pen.Dispose();
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.Quadrangle)
            {
                g.FillRectangle(back_sb, item.BackRectF);

                Region source_r = g.Clip;
                Region current_r = new Region(item.BackRectF);
                g.Clip = current_r;
                float x = this.ItemAutoWidth ? item.BackRectF.X + scale_left_margin : item.BackRectF.X + (item.BackRectF.Width - item.TextSize.Width) / 2;
                g.DrawString(item.Text, this.Font, text_sb, x, item.BackRectF.Y + scale_top_margin, StringFormat.GenericTypographic);
                g.Clip = source_r;
                current_r.Dispose();

                if (isactivated)
                {
                    Pen activated_border_pen = new Pen(this.StyleAppearance.ActivateColor) { DashStyle = DashStyle.Dash };
                    g.DrawRectangle(activated_border_pen, item.BackRectF.X, item.BackRectF.Y, item.BackRectF.Width, item.BackRectF.Height);
                    activated_border_pen.Dispose();
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.Symbol)
            {
                g.FillRectangle(back_sb, item.BackRectF);

                Region source_r = g.Clip;
                Region current_r = new Region(item.BackRectF);
                g.Clip = current_r;
                float x = this.ItemAutoWidth ? item.BackRectF.X + scale_left_margin : item.BackRectF.X + (item.BackRectF.Width - item.TextSize.Width) / 2;
                g.DrawString(item.Text, this.Font, text_sb, x, item.BackRectF.Y + scale_top_margin, StringFormat.GenericTypographic);
                g.Clip = source_r;
                current_r.Dispose();

                if (isactivated)
                {
                    Pen activated_border_pen = new Pen(this.StyleAppearance.ActivateColor) { DashStyle = DashStyle.Dash };
                    g.DrawRectangle(activated_border_pen, item.BackRectF.X, item.BackRectF.Y, item.BackRectF.Width, item.BackRectF.Height);
                    activated_border_pen.Dispose();
                }

                if (itemIndex < this.Items.Count - 1)
                {
                    float w = item.SymbolRectF.Width - 4;
                    symbol_pen.Width = 2;
                    symbol_pen.StartCap = LineCap.Round;
                    symbol_pen.EndCap = LineCap.Round;
                    g.DrawLine(symbol_pen, item.SymbolRectF.X, item.SymbolRectF.Y, item.SymbolRectF.X + w / 6 * 5, item.SymbolRectF.Y + item.SymbolRectF.Height / 2);
                    g.DrawLine(symbol_pen, item.SymbolRectF.X + w / 6 * 5, item.SymbolRectF.Y + item.SymbolRectF.Height / 2, item.SymbolRectF.X, item.SymbolRectF.Y + item.SymbolRectF.Height);

                    g.DrawLine(symbol_pen, item.SymbolRectF.X + w / 6 + 4, item.SymbolRectF.Y, item.SymbolRectF.X + w + 4, item.SymbolRectF.Y + item.SymbolRectF.Height / 2);
                    g.DrawLine(symbol_pen, item.SymbolRectF.X + w + 4, item.SymbolRectF.Y + item.SymbolRectF.Height / 2, item.SymbolRectF.X + w / 6 + 4, item.SymbolRectF.Y + item.SymbolRectF.Height);
                }
            }
            else if (this.StylePattern == BreadcrumbBarStylePattern.ObliqueLine)
            {
                g.FillRectangle(back_sb, item.BackRectF);

                Region source_r = g.Clip;
                Region current_r = new Region(item.BackRectF);
                g.Clip = current_r;
                float x = this.ItemAutoWidth ? item.BackRectF.X + scale_left_margin : item.BackRectF.X + (item.BackRectF.Width - item.TextSize.Width) / 2;
                g.DrawString(item.Text, this.Font, text_sb, x, item.BackRectF.Y + scale_top_margin, StringFormat.GenericTypographic);
                g.Clip = source_r;
                current_r.Dispose();

                if (isactivated)
                {
                    Pen activated_border_pen = new Pen(this.StyleAppearance.ActivateColor) { DashStyle = DashStyle.Dash };
                    g.DrawRectangle(activated_border_pen, item.BackRectF.X, item.BackRectF.Y, item.BackRectF.Width, item.BackRectF.Height);
                    activated_border_pen.Dispose();
                }

                if (itemIndex < this.Items.Count - 1)
                {
                    symbol_pen.Width = 2;
                    symbol_pen.StartCap = LineCap.Round;
                    symbol_pen.EndCap = LineCap.Round;
                    g.DrawLine(symbol_pen,
                        item.SymbolRectF.X + 2,
                        item.SymbolRectF.Y + 2,
                        item.SymbolRectF.Right - 2,
                        item.SymbolRectF.Bottom - 2
                        );
                }
            }
        }

        /// <summary>
        /// 根据字体计算控件高度
        /// </summary>
        /// <returns></returns>
        private int GetControlAutoHeight()
        {
            NativeMethods.TEXTMETRIC font_metrics = ControlHelper.GetFontMetrics(this.Handle, this.Font);
            return this.content_padding + (int)Math.Ceiling(this.ItemTextTBMargin.Top * this.ScaleDpi) + font_metrics.GetFontRealHeight() + (int)Math.Ceiling(this.ItemTextTBMargin.Bottom * this.ScaleDpi) + this.content_padding;
        }

        #endregion

        #region 外观

        public class StyleAppearanceObject : AppearanceObjectBase
        {
            internal StyleAppearanceObject(ISkinObject owner, AppearanceObjectBase parent) : base(owner, parent) { }

            #region 属性

            private Color skinActivateColor = Color.Empty;
            private readonly Color defaultActivateColor = SystemColors.Window;
            private Color activateColor = Color.Empty;
            /// <summary>
            /// 控件激活的虚线框颜色
            /// </summary>
            [Description("控件激活的虚线框颜色")]
            [PropertyOrder(-200)]
            [SkinProperty(true, false)]
            public Color ActivateColor
            {
                get
                {
                    if (this.owner.GetSkinObjectSkinStateCore())
                    {
                        return this.skinActivateColor;
                    }

                    if (this.activateColor != Color.Empty)
                    {
                        return this.activateColor;
                    }

                    return this.defaultActivateColor;
                }
                set
                {
                    if (this.activateColor == value)
                        return;

                    this.activateColor = value;
                    this.Invalidate();
                }
            }
            private bool ShouldSerializeActivateColor()
            {
                return this.activateColor != Color.Empty;
            }
            private void ResetActivateColor()
            {
                this.activateColor = Color.Empty;
                this.Invalidate();
            }

            private Color skinBackColor = Color.Empty;
            private readonly Color defaultBackColor = SystemColors.Control;
            private Color backColor = Color.Empty;
            /// <summary>
            /// 背景颜色
            /// </summary>
            [Description("背景颜色")]
            [PropertyOrder(-195)]
            [SkinProperty(true, true)]
            public Color BackColor
            {
                get
                {
                    if (this.owner.GetSkinObjectSkinStateCore() && this.skinBackColor != Color.Empty)
                    {
                        return this.skinBackColor;
                    }

                    if (this.backColor != Color.Empty)
                    {
                        return this.backColor;
                    }

                    if (((Control)this.owner).Parent != null)
                    {
                        return ((Control)this.owner).Parent.BackColor;
                    }

                    return this.defaultBackColor;
                }
                set
                {
                    if (this.backColor == value)
                        return;

                    this.backColor = value;
                    this.Invalidate();
                }
            }
            private bool ShouldSerializeBackColor()
            {
                return this.backColor != Color.Empty;
            }
            private void ResetBackColor()
            {
                this.backColor = Color.Empty;
                this.Invalidate();
            }

            private StateAppearanceObject unSelected;
            /// <summary>
            /// 未选中状态外观
            /// </summary>
            [Description("未选中状态外观")]
            [PropertyOrder(-178)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            [SkinProperty(true, false)]
            public StateAppearanceObject UnSelected
            {
                get
                {
                    if (this.unSelected == null)
                        this.unSelected = new StateAppearanceObject(this.owner, this
                            , SystemColors.ActiveCaption
                            , SystemColors.GradientActiveCaption
                            , SystemColors.ButtonHighlight
                            , SystemColors.ActiveCaption
                            , SystemColors.ActiveCaption
                            , SystemColors.ButtonHighlight
                            , SystemColors.InactiveCaption
                            , SystemColors.GradientInactiveCaption
                            , SystemColors.InactiveBorder
                            );
                    return this.unSelected;
                }
            }

            private StateAppearanceObject selected;
            /// <summary>
            /// 已选中状态外观
            /// </summary>
            [Description("已选中状态外观")]
            [PropertyOrder(-176)]
            [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
            [SkinProperty(true, false)]
            public StateAppearanceObject Selected
            {
                get
                {
                    if (this.selected == null)
                        this.selected = new StateAppearanceObject(this.owner, this
                            , SystemColors.ActiveCaption
                            , SystemColors.ActiveCaption
                            , SystemColors.ButtonHighlight
                            , SystemColors.ActiveCaption
                            , SystemColors.ActiveCaption
                            , SystemColors.ButtonHighlight
                            , SystemColors.InactiveCaption
                            , SystemColors.InactiveCaption
                            , SystemColors.InactiveBorder
                            );
                    return this.selected;
                }
            }

            #endregion

            public class StateAppearanceObject : AppearanceObjectBase
            {
                internal StateAppearanceObject(ISkinObject owner, AppearanceObjectBase parent, Color backNormalColor, Color symbolNormalColor, Color textNormalColor, Color symbolEnterColor, Color backEnterColor, Color textEnterColor, Color symbolDisabledColor, Color backDisabledColor, Color textDisabledColor) : base(owner, parent)
                {
                    this.backNormalColor = backNormalColor;
                    this.textNormalColor = textNormalColor;
                    this.backEnterColor = backEnterColor;
                    this.textEnterColor = textEnterColor;
                    this.backDisabledColor = backDisabledColor;
                    this.textDisabledColor = textDisabledColor;
                }

                #region 属性

                private readonly Color symbolNormalColor = Color.Empty;
                private readonly Color backNormalColor = Color.Empty;
                private Color textNormalColor = Color.Empty;
                private ItemAppearanceObject normal;
                /// <summary>
                /// 正常状态
                /// </summary>
                [Description("正常状态")]
                [PropertyOrder(-194)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                [SkinProperty(true, false)]
                public ItemAppearanceObject Normal
                {
                    get
                    {
                        if (this.normal == null)
                            this.normal = new ItemAppearanceObject(this.owner, this, this.symbolNormalColor, this.backNormalColor, this.textNormalColor);
                        return this.normal;
                    }
                }

                private readonly Color symbolEnterColor = Color.Empty;
                private readonly Color backEnterColor = Color.Empty;
                private Color textEnterColor = Color.Empty;
                private ItemAppearanceObject enter;
                /// <summary>
                /// 鼠标进入状态
                /// </summary>
                [Description("鼠标进入状态")]
                [PropertyOrder(-192)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                [SkinProperty(true, false)]
                public ItemAppearanceObject Enter
                {
                    get
                    {
                        if (this.enter == null)
                            this.enter = new ItemAppearanceObject(this.owner, this, this.symbolEnterColor, this.backEnterColor, this.textEnterColor);
                        return this.enter;
                    }
                }

                private readonly Color symbolDisabledColor = Color.Empty;
                private readonly Color backDisabledColor = Color.Empty;
                private Color textDisabledColor = Color.Empty;
                private ItemAppearanceObject disabled;
                /// <summary>
                /// 禁用状态
                /// </summary>
                [Description("禁用状态")]
                [PropertyOrder(-190)]
                [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
                [SkinProperty(true, false)]
                public ItemAppearanceObject Disabled
                {
                    get
                    {
                        if (this.disabled == null)
                            this.disabled = new ItemAppearanceObject(this.owner, this, this.symbolDisabledColor, this.backDisabledColor, this.textDisabledColor);
                        return this.disabled;
                    }
                }

                #endregion

                public class ItemAppearanceObject : AppearanceObjectBase
                {
                    internal ItemAppearanceObject(ISkinObject owner, AppearanceObjectBase parent, Color borderColor, Color backColor, Color textColor) : base(owner, parent)
                    {
                        this.defaultSymbolColor = borderColor;
                        this.defaultBackColor = backColor;
                        this.defaultTextColor = textColor;
                    }

                    #region 属性

                    private Color skinBackColor = Color.Empty;
                    private readonly Color defaultBackColor = Color.Empty;
                    private Color backColor = Color.Empty;
                    /// <summary>
                    /// 背景颜色
                    /// </summary>
                    [Description("背景颜色")]
                    [PropertyOrder(-158)]
                    [Editor(typeof(AlphaColorEditor), typeof(UITypeEditor))]
                    [SkinProperty(true, false)]
                    public Color BackColor
                    {
                        get
                        {
                            if (this.owner.GetSkinObjectSkinStateCore())
                            {
                                return this.skinBackColor;
                            }

                            if (this.backColor != Color.Empty)
                            {
                                return this.backColor;
                            }

                            return this.defaultBackColor;
                        }
                        set
                        {
                            if (this.backColor == value)
                                return;

                            this.backColor = value;
                            this.Invalidate();
                        }
                    }
                    private bool ShouldSerializeBackColor()
                    {
                        return this.backColor != Color.Empty;
                    }
                    private void ResetBackColor()
                    {
                        this.backColor = Color.Empty;
                        this.Invalidate();
                    }

                    private Color skinTextColor = Color.Empty;
                    private readonly Color defaultTextColor = Color.Empty;
                    private Color textColor = Color.Empty;
                    /// <summary>
                    /// 文本颜色
                    /// </summary>
                    [Description("文本颜色")]
                    [PropertyOrder(-157)]
                    [Editor(typeof(AlphaColorEditor), typeof(UITypeEditor))]
                    [SkinProperty(true, false)]
                    public Color TextColor
                    {
                        get
                        {
                            if (this.owner.GetSkinObjectSkinStateCore())
                            {
                                return this.skinTextColor;
                            }

                            if (this.textColor != Color.Empty)
                            {
                                return this.textColor;
                            }

                            return this.defaultTextColor;
                        }
                        set
                        {
                            if (this.textColor == value)
                                return;

                            this.textColor = value;
                            this.Invalidate();
                        }
                    }
                    private bool ShouldSerializeTextColor()
                    {
                        return this.textColor != Color.Empty;
                    }
                    private void ResetTextColor()
                    {
                        this.textColor = Color.Empty;
                        this.Invalidate();
                    }

                    private Color skinSymbolColor = Color.Empty;
                    private readonly Color defaultSymbolColor = Color.Empty;
                    private Color symbolColor = Color.Empty;
                    /// <summary>
                    /// 选项分隔符号颜色(只限于 Symbol、ObliqueLine)
                    /// </summary>
                    [Description("选项分隔符号颜色(只限于 Symbol、ObliqueLine)")]
                    [PropertyOrder(-156)]
                    [SkinProperty(true, false)]
                    public Color SymbolColor
                    {
                        get
                        {
                            if (this.owner.GetSkinObjectSkinStateCore())
                            {
                                return this.skinSymbolColor;
                            }

                            if (this.symbolColor != Color.Empty)
                            {
                                return this.symbolColor;
                            }

                            return this.defaultSymbolColor;
                        }
                        set
                        {
                            if (this.symbolColor == value)
                                return;

                            this.symbolColor = value;
                            this.Invalidate();
                        }
                    }
                    private bool ShouldSerializeSymbolColor()
                    {
                        return this.symbolColor != Color.Empty;
                    }
                    private void ResetSymbolColor()
                    {
                        this.symbolColor = Color.Empty;
                        this.Invalidate();
                    }

                    #endregion

                }
            }

        }

        #endregion

    }

}
